home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gchess40.lha / gnuchess4.0p62 / src / dspcom.c < prev    next >
C/C++ Source or Header  |  1993-06-24  |  33KB  |  1,430 lines

  1. /*
  2.  * dspcom.c - C source for GNU CHESS
  3.  *
  4.  * Copyright (c) 1988,1989,1990 John Stanback
  5.  * Copyright (c) 1992 Free Software Foundation
  6.  *
  7.  * This file is part of GNU CHESS.
  8.  *
  9.  * GNU Chess is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2, or (at your option)
  12.  * any later version.
  13.  *
  14.  * GNU Chess is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with GNU Chess; see the file COPYING.  If not, write to
  21.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23. #define DEBUG8 1
  24. #include "gnuchess.h"
  25. #include "ataks.h"
  26. #ifdef HASGETTIMEOFDAY
  27. #include <sys/time.h>
  28. #endif
  29. extern short Mwpawn[64], Mbpawn[64], Mknight[2][64], Mbishop[2][64];
  30. extern char *version, *patchlevel;
  31. extern unsigned int TTadd;
  32. extern unsigned int ttbllimit;
  33. char mvstr[4][6];
  34. char *InPtr;
  35. int    InBackground = false;
  36.  
  37.  
  38. #include <ctype.h>
  39. #include <signal.h>
  40. #if defined(MSDOS)
  41. #include <dos.h>
  42. /*#include <conio.h>*/
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include <time.h>
  46. #else
  47. #include <sys/param.h>
  48. #include <sys/types.h>
  49. #include <sys/file.h>
  50. #include <sys/ioctl.h>
  51. #endif
  52. void
  53. algbr (short int f, short int t, short int flag)
  54.  
  55.  
  56. /*
  57.  * Generate move strings in different formats.
  58.  */
  59.  
  60. {
  61.   int m3p;
  62.  
  63.   if (f != t)
  64.     {
  65.       /* algebraic notation */
  66.       mvstr[0][0] = cxx[column (f)];
  67.       mvstr[0][1] = rxx[row (f)];
  68.       mvstr[0][2] = cxx[column (t)];
  69.       mvstr[0][3] = rxx[row (t)];
  70.       mvstr[0][4] = mvstr[3][0] = '\0';
  71.       if (((mvstr[1][0] = pxx[board[f]]) == CP[2][1]) || (flag & promote))
  72.     {
  73.       if (mvstr[0][0] == mvstr[0][2])    /* pawn did not eat */
  74.         {
  75.           mvstr[2][0] = mvstr[1][0] = mvstr[0][2];    /* to column */
  76.           mvstr[2][1] = mvstr[1][1] = mvstr[0][3];    /* to row */
  77.           m3p = 2;
  78.         }
  79.       else
  80.         /* pawn ate */
  81.         {
  82.           mvstr[2][0] = mvstr[1][0] = mvstr[0][0];    /* column */
  83.           mvstr[2][1] = mvstr[1][1] = mvstr[0][2];    /* to column */
  84.           mvstr[2][2] = mvstr[0][3];
  85.           m3p = 3;        /* to row */
  86.         }
  87.       if (flag & promote)
  88.         {
  89. #if !defined CLIENT
  90.           mvstr[0][4] = mvstr[1][2] = mvstr[2][m3p] = qxx[flag & pmask];
  91.           mvstr[0][5] = mvstr[1][3] = mvstr[2][m3p + 1] = mvstr[3][0] = '\0';
  92. #ifdef CHESSTOOL 
  93.           mvstr[3][0] = mvstr[0][0];    /* Allow e7e8 for chesstool */
  94.           mvstr[3][1] = mvstr[0][1];
  95.           mvstr[3][2] = mvstr[0][2];
  96.           mvstr[3][3] = mvstr[0][3];
  97.           mvstr[3][4] = '\0';
  98. #endif
  99. #endif
  100.         } else mvstr[2][m3p] = mvstr[1][2] = '\0';
  101.     }
  102.       else
  103.     /* not a pawn */
  104.     {
  105.       mvstr[2][0] = mvstr[1][0];
  106.       mvstr[2][1] = mvstr[0][1];
  107.       mvstr[2][2] = mvstr[1][1] = mvstr[0][2];    /* to column */
  108.       mvstr[2][3] = mvstr[1][2] = mvstr[0][3];    /* to row */
  109.       mvstr[2][4] = mvstr[1][3] = '\0';
  110.       strcpy (mvstr[3], mvstr[2]);
  111.           mvstr[3][1] = mvstr[0][0];
  112.       if (flag & cstlmask)
  113.         {
  114.           if (t > f)
  115.         {
  116.           strcpy (mvstr[1], mvstr[0]);
  117.           strcpy (mvstr[0], CP[5]);
  118.           strcpy (mvstr[2], CP[7]);
  119.         }
  120.           else
  121.         {
  122.           strcpy (mvstr[1], mvstr[0]);
  123.           strcpy (mvstr[0], CP[6]);
  124.           strcpy (mvstr[2], CP[8]);
  125.         }
  126.         }
  127.     }
  128.     }
  129.   else
  130.     mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
  131. }
  132.  
  133.  
  134. int
  135. VerifyMove (char *s, short int iop, short unsigned int *mv)
  136.  
  137. /*
  138.  * Compare the string 's' to the list of legal moves available for the
  139.  * opponent. If a match is found, make the move on the board.
  140.  */
  141.  
  142. {
  143.   static short pnt, tempb, tempc, tempsf, tempst, cnt;
  144.   static struct leaf xnode;
  145.   struct leaf *node;
  146.   *mv = 0;
  147.  
  148.   if (iop == 2)
  149.     {
  150.       UnmakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
  151.       return (false);
  152.     }
  153.   cnt = 0;
  154.   MoveList (opponent, 2);
  155.   pnt = TrPnt[2];
  156.   while (pnt < TrPnt[3])
  157.     {
  158.       node = &Tree[pnt++];
  159.       algbr (node->f, node->t, (short) node->flags);
  160.       if (strcmp (s, mvstr[0]) == 0 || strcmp (s, mvstr[1]) == 0 ||
  161.       strcmp (s, mvstr[2]) == 0 || strcmp (s, mvstr[3]) == 0)
  162.     {
  163.       cnt++;
  164.       xnode = *node;
  165.     }
  166.     }
  167.   if (cnt == 1)
  168.     {
  169.       MakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst, &INCscore);
  170.       if (SqAtakd (PieceList[opponent][0], computer))
  171.     {
  172.       UnmakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
  173. #if defined CHESSTOOL
  174.       printz (CP[15]);
  175. #else
  176. #ifdef NONDSP
  177. /* Illegal move in check */
  178.       printz (CP[77]);
  179.       printz ("\n");
  180. #else
  181. /* Illegal move in check */
  182.       ShowMessage (CP[77]);
  183. #endif
  184. #endif /* CHESSTOOL */
  185.       return (false);
  186.     }
  187.       else
  188.     {
  189.       if (iop == 1)
  190.         return (true);
  191.       UpdateDisplay (xnode.f, xnode.t, 0, (short) xnode.flags);
  192.       if ((board[xnode.t] == pawn)
  193.           || (xnode.flags & capture)
  194.           || (xnode.flags & cstlmask))
  195.         {
  196.           Game50 = GameCnt;
  197.           ZeroRPT ();
  198.         }
  199.       GameList[GameCnt].depth = GameList[GameCnt].score = 0;
  200.       GameList[GameCnt].nodes = 0;
  201.       ElapsedTime (1);
  202.       GameList[GameCnt].time = (short) (et+50)/100;
  203.       if (TCflag)
  204.         {
  205.           TimeControl.clock[opponent] -= et;
  206.           timeopp[oppptr] = et;
  207.           --TimeControl.moves[opponent];
  208.         }
  209.       *mv = (xnode.f << 8) | xnode.t;
  210.       algbr (xnode.f, xnode.t, false);
  211.       return (true);
  212.     }
  213.     }
  214. #if defined CHESSTOOL
  215.   printz (CP[78]);
  216. #else
  217. #ifdef NONDSP
  218. /* Illegal move */
  219.   printz (CP[75], s);
  220. #ifdef DEBUG8
  221.   if (1)
  222.     {
  223.       FILE *D;
  224.       int r, c, l;
  225.       extern unsigned short int PrVar[];
  226.       D = fopen ("/tmp/DEBUG", "a+");
  227.       pnt = TrPnt[2];
  228.       fprintf (D, "resp = %d\n", ResponseTime);
  229.       fprintf (D, "iop = %d\n", iop);
  230.       fprintf (D, "matches = %d\n", cnt);
  231.       algbr (hint >> 8, hint & 0xff, (short) 0);
  232.       fprintf (D, "hint %s\n", mvstr[0]);
  233.       fprintf (D, "inout move is %s\n", s);
  234.       for (r = 1; PrVar[r]; r++)
  235.     {
  236.       algbr (PrVar[r] >> 8, PrVar[r] & 0xff, (short) 0);
  237.       fprintf (D, " %s", mvstr[0]);
  238.     }
  239.       fprintf (D, "\n");
  240.       fprintf (D, "legal move are \n");
  241.       while (pnt < TrPnt[3])
  242.     {
  243.       node = &Tree[pnt++];
  244.       algbr (node->f, node->t, (short) node->flags);
  245.       fprintf (D, "%s %s %s %s\n", mvstr[0], mvstr[1], mvstr[2], mvstr[3]);
  246.     }
  247.       fprintf (D, "\n current board is\n");
  248.       for (r = 7; r >= 0; r--)
  249.     {
  250.       for (c = 0; c <= 7; c++)
  251.         {
  252.           l = locn (r, c);
  253.           if (color[l] == neutral)
  254.         fprintf (D, " -");
  255.           else if (color[l] == white)
  256.         fprintf (D, " %c", qxx[board[l]]);
  257.           else
  258.         fprintf (D, " %c", pxx[board[l]]);
  259.         }
  260.       fprintf (D, "\n");
  261.     }
  262.       fprintf (D, "\n");
  263.       fclose (D);
  264.     }
  265. #endif
  266. #else
  267. /* Illegal move */
  268.   ShowMessage (CP[76]);
  269. #endif
  270. #endif /* CHESSTOOL */
  271. #if !defined CHESSTOOL && !defined XBOARD
  272.   if (cnt > 1)
  273.     ShowMessage (CP[32]);
  274. #endif /* CHESSTOOL */
  275.   return (false);
  276. }
  277.  
  278. int
  279. parser (char *f, int side)
  280. {
  281.   int c1, r1, c2, r2;
  282.  
  283.   if (f[4] == 'o')
  284.     if (side == black)
  285.       return 0x3C3A;
  286.     else
  287.       return 0x0402;
  288.   else if (f[0] == 'o')
  289.     if (side == black)
  290.       return 0x3C3E;
  291.     else
  292.       return 0x0406;
  293.   else
  294.     {
  295.       c1 = f[0] - 'a';
  296.       r1 = f[1] - '1';
  297.       c2 = f[2] - 'a';
  298.       r2 = f[3] - '1';
  299.       return (locn (r1, c1) << 8) | locn (r2, c2);
  300.     }
  301.   /*NOTREACHED*/
  302. }
  303.  
  304. void
  305. GetGame (void)
  306. {
  307.   FILE *fd;
  308.   char fname[256], *p;
  309.   int c, i, j;
  310.   int eps = -1;
  311.   short sq;
  312. /* enter file name */
  313.   ShowMessage (CP[63]);
  314.   scanz ("%s", fname);
  315. /* chess.000 */
  316.   if (fname[0] == '\0')
  317.     strcpy (fname, CP[137]);
  318.   if ((fd = fopen (fname, "r")) != NULL)
  319.     {
  320.       NewGame ();
  321.       fgets (fname, 256, fd);
  322.       computer = opponent = white;
  323.       InPtr = fname;
  324.       skip ();
  325.       if (*InPtr == 'c')
  326.     computer = black;
  327.       else
  328.     opponent = black;
  329.       skip ();
  330.       skip ();
  331.       skip ();
  332.       Game50 = atoi (InPtr);
  333.       skip();
  334.       skip();
  335.       eps = atoi(InPtr);
  336.       fgets (fname, 256, fd);
  337.       InPtr = &fname[14];
  338.       castld[white] = ((*InPtr == CP[214][0]) ? true : false);
  339.       skip ();
  340.       skip ();
  341.       castld[black] = ((*InPtr == CP[214][0]) ? true : false);
  342.       fgets (fname, 256, fd);
  343.       InPtr = &fname[11];
  344.       skipb ();
  345.       TCflag = atoi (InPtr);
  346.       skip ();
  347.       InPtr += 14;
  348.       skipb ();
  349.       OperatorTime = atoi (InPtr);
  350.       fgets (fname, 256, fd);
  351.       InPtr = &fname[11];
  352.       skipb ();
  353.       TimeControl.clock[white] = atol (InPtr);
  354.       skip ();
  355.       skip ();
  356.       TimeControl.moves[white] = atol (InPtr);
  357.       fgets (fname, 256, fd);
  358.       InPtr = &fname[11];
  359.       skipb ();
  360.       TimeControl.clock[black] = atoi (InPtr);
  361.       skip ();
  362.       skip ();
  363.       TimeControl.moves[black] = atoi (InPtr);
  364.       fgets (fname, 256, fd);
  365.       for (i = 7; i > -1; i--)
  366.     {
  367.       fgets (fname, 256, fd);
  368.       p = &fname[2];
  369.       InPtr = &fname[11];
  370.       skipb ();
  371.       for (j = 0; j < 8; j++)
  372.         {
  373.           sq = i * 8 + j;
  374.           if (*p == '.')
  375.         {
  376.           board[sq] = no_piece;
  377.           color[sq] = neutral;
  378.         }
  379.           else
  380.         {
  381.           for (c = 0; c < 8; c++)
  382.             {
  383.               if (*p == pxx[c])
  384.             {
  385.               board[sq] = c;
  386.               color[sq] = black;
  387.             }
  388.             }
  389.           for (c = 0; c < 8; c++)
  390.             {
  391.               if (*p == qxx[c])
  392.             {
  393.               board[sq] = c;
  394.               color[sq] = white;
  395.             }
  396.             }
  397.         }
  398.           p++;
  399.           Mvboard[sq] = atoi (InPtr);
  400.           skip ();
  401.         }
  402.     }
  403.       GameCnt = 0;
  404.       flag.regularstart = true;
  405.       Book = BOOKFAIL;
  406.       fgets (fname, 256, fd);
  407.       fgets (fname, 256, fd);
  408.       fgets (fname, 256, fd);
  409.       while (fgets (fname, 256, fd))
  410.     {
  411.       struct GameRec *g;
  412.       int side = computer;
  413.  
  414.       side = side ^ 1;
  415.       ++GameCnt;
  416.       InPtr = fname;
  417.       skipb ();
  418.       g = &GameList[GameCnt];
  419.       g->gmove = parser (InPtr, side);
  420.       skip ();
  421.       g->score = atoi (InPtr);
  422.       skip ();
  423.       g->depth = atoi (InPtr);
  424.       skip ();
  425.       g->nodes = atol (InPtr);
  426.       skip ();
  427.       g->time = atol (InPtr);
  428.       skip ();
  429.       g->flags = c = atoi (InPtr);
  430.       skip ();
  431.       g->hashkey = strtol (InPtr, (char **) NULL, 16);
  432.       skip ();
  433.       g->hashbd = strtol (InPtr, (char **) NULL, 16);
  434.       skip ();
  435.       g->epssq = atoi(InPtr);
  436.       g->piece = no_piece;
  437.       g->color = neutral;
  438.       if (c & (capture | cstlmask))
  439.         {
  440.           if (c & capture)
  441.         {
  442.           skip ();
  443.           for (c = 0; c < 8; c++)
  444.             if (pxx[c] == *InPtr)
  445.               break;
  446.           g->piece = c;
  447.         }
  448.           skip ();
  449.           g->color = ((*InPtr == CP[119][0]) ? black : white);
  450.         }
  451.     }
  452.       if (TimeControl.clock[white] > 0)
  453.     TCflag = true;
  454.       fclose (fd);
  455.     }
  456.   ZeroRPT ();
  457.   InitializeStats ();
  458.   epsquare = eps;
  459.   UpdateDisplay (0, 0, 1, 0);
  460.   Sdepth = 0;
  461.   hint = 0;
  462. }
  463.  
  464. void
  465. GetXGame (void)
  466. {
  467.   FILE *fd;
  468.   char fname[256], *p;
  469.   int c, i, j;
  470.   short sq;
  471. /* Enter file name */
  472.   ShowMessage (CP[63]);
  473.   scanz ("%s", fname);
  474.   if (fname[0] == '\0')
  475. /* xboard.position.read*/
  476.     strcpy (fname, CP[205]);
  477.   if ((fd = fopen (fname, "r")) != NULL)
  478.     {
  479.       NewGame ();
  480.       flag.regularstart = false;
  481.       Book = false;
  482.       fgets (fname, 256, fd);
  483. #ifdef notdef
  484.       fname[6] = '\0';
  485.       if (strcmp (fname, CP[206]))
  486.     return;
  487. #endif
  488.       fgets (fname, 256, fd);
  489.       fgets (fname, 256, fd);
  490.       for (i = 7; i > -1; i--)
  491.     {
  492.       fgets (fname, 256, fd);
  493.       p = fname;
  494.       for (j = 0; j < 8; j++)
  495.         {
  496.           sq = i * 8 + j;
  497.           if (*p == '.')
  498.         {
  499.           board[sq] = no_piece;
  500.           color[sq] = neutral;
  501.         }
  502.           else
  503.         {
  504.           for (c = 0; c < 8; c++)
  505.             {
  506.               if (*p == qxx[c])
  507.             {
  508.               board[sq] = c;
  509.               color[sq] = black;
  510.             }
  511.             }
  512.           for (c = 0; c < 8; c++)
  513.             {
  514.               if (*p == pxx[c])
  515.             {
  516.               board[sq] = c;
  517.               color[sq] = white;
  518.             }
  519.             }
  520.         }
  521.           p += 2;
  522.         }
  523.     }
  524.         if (fgets(fname, 256, fd) != NULL && strncmp(fname, "black", 5) == 0)
  525.         {
  526.           computer = white;
  527.           opponent = black;
  528.           xwndw = BXWNDW;
  529.     }
  530.       fclose (fd);
  531.     }
  532.   ZeroRPT ();
  533.   InitializeStats ();
  534.   UpdateDisplay (0, 0, 1, 0);
  535.   Sdepth = 0;
  536.   hint = 0;
  537. }
  538.  
  539. void
  540. SaveGame (void)
  541. {
  542.   FILE *fd;
  543.   char fname[256];
  544.   short sq, i, c, f, t;
  545.   char p;
  546.  
  547.   if (savefile[0])
  548.     strcpy (fname, savefile);
  549.   else
  550.     {
  551. /* Enter file name*/
  552.       ShowMessage (CP[63]);
  553.       scanz ("%s", fname);
  554.     }
  555.  
  556.   if (fname[0] == '\0')
  557. /* chess.000 */
  558.     strcpy (fname, CP[137]);
  559.   if ((fd = fopen (fname, "w")) != NULL)
  560.     {
  561.       char *b, *w;
  562.  
  563.       b = w = CP[74];
  564.       if (computer == black)
  565.     b = CP[141];
  566.       if (computer == white)
  567.     w = CP[141];
  568.       fprintf (fd, CP[37], b, w, Game50,epsquare);
  569.       fprintf (fd, CP[42], castld[white] ? CP[214] : CP[215], castld[black] ? CP[214] : CP[215]);
  570.       fprintf (fd, CP[111], TCflag, OperatorTime);
  571.       fprintf (fd, CP[117],
  572.            TimeControl.clock[white], TimeControl.moves[white],
  573.            TimeControl.clock[black], TimeControl.moves[black]);
  574.       for (i = 7; i > -1; i--)
  575.     {
  576.       fprintf (fd, "%1d ", i + 1);
  577.       for (c = 0; c < 8; c++)
  578.         {
  579.           sq = i * 8 + c;
  580.           switch (color[sq])
  581.         {
  582.         case black:
  583.           p = pxx[board[sq]];
  584.           break;
  585.         case white:
  586.           p = qxx[board[sq]];
  587.           break;
  588.         default:
  589.           p = '.';
  590.         }
  591.           fprintf (fd, "%c", p);
  592.         }
  593.       for (f = i * 8; f < i * 8 + 8; f++)
  594.         fprintf (fd, " %d", Mvboard[f]);
  595.       fprintf (fd, "\n");
  596.     }
  597.       fprintf (fd, "  %s\n", cxx);
  598.       fprintf (fd, CP[126]);
  599.       for (i = 1; i <= GameCnt; i++)
  600.     {
  601.       struct GameRec *g = &GameList[i];
  602.  
  603.       f = g->gmove >> 8;
  604.       t = (g->gmove & 0xFF);
  605.       algbr (f, t, g->flags);
  606.       fprintf (fd, "%s %5d %5d %7ld %6ld %5d  %#08lx %#08lx %d %c   %s\n",
  607.            mvstr[0], g->score, g->depth,
  608.            g->nodes, g->time, g->flags, g->hashkey, g->hashbd,g->epssq,
  609.        pxx[g->piece], ((g->color == 2) ? "     " : ColorStr[g->color]));
  610.     }
  611.       fclose (fd);
  612. /* Game saved */
  613.       ShowMessage (CP[70]);
  614.     }
  615.   else
  616.     /*ShowMessage ("Could not open file");*/
  617.     ShowMessage (CP[48]);
  618. }
  619.  
  620. void
  621. ListGame (void)
  622. {
  623.   FILE *fd;
  624.   short i, f, t;
  625. #ifndef MSDOS
  626.   time_t when;
  627.   char fname[256], dbuf[256];
  628. #else
  629.   char fname[256];
  630. #endif
  631.  
  632.   if (listfile[0])
  633.     strcpy (fname, listfile);
  634.   else
  635.     {
  636. #ifdef MSDOS
  637.       sprintf (fname, "chess.lst");
  638. #else
  639.       time (&when);
  640.       strncpy (dbuf, ctime (&when), 20);
  641.       dbuf[7] = '\0';
  642.       dbuf[10] = '\0';
  643.       dbuf[13] = '\0';
  644.       dbuf[16] = '\0';
  645.       dbuf[19] = '\0';
  646. /* use format "CLp16.Jan01-020304B" when patchlevel is 16,
  647.    date is Jan 1
  648.    time is 02:03:04
  649.    program played black */
  650.       sprintf (fname, "CLp%s.%s%s-%s%s%s%c", patchlevel, dbuf + 4, dbuf + 8, dbuf + 11, dbuf + 14, dbuf + 17, ColorStr[computer][0]);
  651.       /* replace space padding with 0 */
  652.       for (i = 0; fname[i] != '\0'; i++)
  653.     if (fname[i] == ' ')
  654.       fname[i] = '0';
  655. #endif /* MSDOS */
  656.     }
  657.   fd = fopen (fname, "w");
  658.   if (!fd)
  659.     {
  660.       printf (CP[219], fname);
  661.       exit (1);
  662.     }
  663.   /*fprintf (fd, "gnuchess game %d\n", u);*/
  664.   fprintf (fd, CP[161], patchlevel);
  665.   fprintf (fd, CP[10]);
  666.   fprintf (fd, CP[11]);
  667.   for (i = 1; i <= GameCnt; i++)
  668.     {
  669.       f = GameList[i].gmove >> 8;
  670.       t = (GameList[i].gmove & 0xFF);
  671.       algbr (f, t, GameList[i].flags);
  672.       if(GameList[i].flags & book)
  673.           fprintf (fd, "%5s  %5d    Book%7ld %5d", mvstr[0],
  674.            GameList[i].score, 
  675.            GameList[i].nodes, GameList[i].time);
  676.       else
  677.           fprintf (fd, "%5s  %5d     %2d %7ld %5d", mvstr[0],
  678.            GameList[i].score, GameList[i].depth,
  679.            GameList[i].nodes, GameList[i].time);
  680.       if ((i % 2) == 0)
  681.     {
  682. #ifdef DEBUG40
  683.     if(computer == black){
  684.       int p;
  685.     for(p=0;GameList[i-1].d1[p] && p < 7;p++){
  686.         algbr((GameList[i-1].d1[p]>>8) & 0xff,GameList[i-1].d1[p] & 0xff,0);
  687.             fprintf(fd," %s",mvstr[0]);
  688.     }
  689.     fprintf(fd,"\n");
  690.     } else {
  691.     int p;
  692.     for(p=0;GameList[i].d1[p] && p < 7;p++){
  693.         algbr(GameList[i].d1[p]>>8 & 0xff,GameList[i].d1[p] & 0xff,0);
  694.             fprintf(fd," %s",mvstr[0]);
  695.     }
  696.     fprintf(fd,"\n");
  697.     }
  698. #else
  699.     fprintf(fd,"\n");
  700. #endif
  701.     }
  702.    }
  703.   fprintf (fd, "\n\n");
  704.   if (GameList[GameCnt].flags & draw)
  705.     {
  706.       fprintf (fd, CP[54], DRAW);
  707.     }
  708.   else if (GameList[GameCnt].score == -9999)
  709.     {
  710.       fprintf (fd, "%s\n", ColorStr[player ]);
  711.     }
  712.   else if (GameList[GameCnt].score == 9998)
  713.     {
  714.       fprintf (fd, "%s\n", ColorStr[player ^ 1]);
  715.     }
  716.   fclose (fd);
  717. }
  718.  
  719. void
  720. Undo (void)
  721.  
  722. /*
  723.  * Undo the most recent half-move.
  724.  */
  725.  
  726. {
  727.   short f, t;
  728.   f = GameList[GameCnt].gmove >> 8;
  729.   t = GameList[GameCnt].gmove & 0xFF;
  730.   if (board[t] == king && distance (t, f) > 1)
  731.     (void) castle (GameList[GameCnt].color, f, t, 2);
  732.   else
  733.     {
  734.       /* Check for promotion: */
  735.       if (GameList[GameCnt].flags & promote)
  736.     {
  737.       board[t] = pawn;
  738.     }
  739.       board[f] = board[t];
  740.       color[f] = color[t];
  741.       board[t] = GameList[GameCnt].piece;
  742.       color[t] = GameList[GameCnt].color;
  743.       if (color[t] != neutral)
  744.     Mvboard[t]--;
  745.       Mvboard[f]--;
  746.     }
  747.   if (GameList[GameCnt].flags & epmask)
  748.     EnPassant (otherside[color[f]], f, t, 2);
  749.   else
  750.     InitializeStats ();
  751. epsquare = GameList[GameCnt].epssq;
  752.   if (TCflag && (TCmoves>1))
  753.     ++TimeControl.moves[color[f]];
  754.   hashkey = GameList[GameCnt].hashkey;
  755.   hashbd = GameList[GameCnt].hashbd;
  756.   GameCnt--;
  757.   computer = computer ^ 1;
  758.   opponent = opponent ^ 1;
  759.   flag.mate = false;
  760.   Sdepth = 0;
  761.   player = player ^ 1;
  762.   ShowSidetoMove ();
  763.   UpdateDisplay (0, 0, 1, 0);
  764.   if (flag.regularstart)
  765.     Book = BOOKFAIL;
  766. }
  767.  
  768. void
  769.  TestSpeed (void (*f) (short int side, short int ply), unsigned j)
  770. {
  771. #ifdef test
  772.   unsigned jj;
  773. #endif
  774.   unsigned i;
  775.   long cnt, rate, t1, t2;
  776. #ifdef HASGETTIMEOFDAY
  777. struct timeval tv;
  778. #endif
  779.  
  780. #ifdef HASGETTIMEOFDAY
  781.   gettimeofday(&tv,NULL);
  782.   t1 = (tv.tv_sec*100+(tv.tv_usec/10000));
  783. #else
  784.   t1 = time (0);
  785. #endif
  786.   for (i = 0; i < j; i++)
  787.     {
  788.       f (opponent, 2);
  789. #ifdef test
  790.     for(jj=TrPnt[2];i<TrPnt[3];jj++)if(!pick(jj,TrPnt[3]-1))break;
  791. #endif
  792.     }
  793. #ifdef HASGETTIMEOFDAY
  794.   gettimeofday(&tv,NULL);
  795.   t2 = (tv.tv_sec*100+(tv.tv_usec/10000));
  796. #else
  797.   t2 = time (0);
  798. #endif
  799.   cnt = j * (TrPnt[3] - TrPnt[2]);
  800.   if (t2 - t1)
  801.     et = (t2 - t1);
  802.   else
  803.     et = 1;
  804.   rate = (((et) ? ((cnt*100) / et) : 0));
  805.   /*printz ("Nodes= %ld Nodes/sec= %ld\n", cnt, rate);*/
  806. #ifdef NONDSP
  807.   printz (CP[91], cnt, rate);
  808. #ifdef DEBUG9
  809.   for (j = TrPnt[2]; j < TrPnt[3]; j++)
  810.     {
  811.       struct leaf *node = &Tree[j];
  812.       algbr (node->f, node->t, node->flags);
  813.       printf ("%s %s %s %s %d %x\n", mvstr[0], mvstr[1], mvstr[2], mvstr[3],node->score,node->flags);
  814.     }
  815. #endif
  816. #else
  817.   ShowNodeCnt (cnt);
  818. #endif
  819. }
  820.  
  821. void
  822.  TestPSpeed (short int (*f) (short int side), unsigned j)
  823. {
  824.   short i;
  825.   long cnt, rate, t1, t2;
  826. #ifdef HASGETTIMEOFDAY
  827. struct timeval tv;
  828. #endif
  829.  
  830. #ifdef HASGETTIMEOFDAY
  831.   gettimeofday(&tv,NULL);
  832.   t1 = (tv.tv_sec*100+(tv.tv_usec/10000));
  833. #else
  834.   t1 = time (0);
  835. #endif
  836.   for (i = 0; i < j; i++)
  837.     {
  838.       (void) f (opponent);
  839.     }
  840. #ifdef HASGETTIMEOFDAY
  841.   gettimeofday(&tv,NULL);
  842.   t2 = (tv.tv_sec*100+(tv.tv_usec/10000));
  843. #else
  844.   t2 = time (0);
  845. #endif
  846.   cnt = j;
  847.   if (t2 - t1)
  848.     et = (t2 - t1);
  849.   else
  850.     et = 1;
  851.   rate = (et) ? ((cnt*100) / et) : 0;
  852.   /*printz ("Nodes= %ld Nodes/sec= %ld\n", cnt, rate);*/
  853. #ifdef NONDSP
  854.   printz (CP[91], cnt, rate);
  855. #else
  856.   ShowNodeCnt (cnt);
  857. #endif
  858. }
  859.  
  860.  
  861. void
  862. SetOppTime (char *s)
  863. {
  864.   char *time;
  865.   register tmp = 0;
  866.   int m, t,sec;
  867.   sec = 0;
  868.   time = &s[strlen (CP[197])];
  869.   t = (int)strtol (time, &time, 10);
  870.   if(*time == ':'){time++; sec=(int)strtol(time, &time,10);}
  871.   m = (int)strtol (time, &time, 10);
  872. #ifdef CLIENT
  873.   if (t && !m){
  874.      tmp = TimeControl.clock[opponent];
  875.      TimeControl.clock[opponent] = t*6000+sec*100;
  876.      timeopp[oppptr] += (tmp - TimeControl.clock[opponent]);
  877. printf("otim %d %d %d %d\n", TimeControl.clock[opponent],TimeControl.moves[opponent],tmp - TimeControl.clock[opponent],TTadd);
  878.     } else TimeControl.clock[opponent] = t*6000+sec*100;
  879. #else
  880.   if (t) TimeControl.clock[opponent] = t;
  881. #endif
  882.   if (m)
  883.     TimeControl.moves[opponent] = m;
  884. #if defined XBOARD && !defined CLIENT
  885.   printz (CP[222], m, t);
  886. #endif
  887. }
  888.  
  889.  
  890.  
  891. void
  892. SetMachineTime (char *s)
  893. {
  894.   char *time;
  895.   long tmp = 0;
  896.   int m, t,sec;
  897.   sec = 0;
  898.   time = &s[strlen (CP[197])];
  899.   t = (int)strtol (time, &time, 10);
  900.   if(*time == ':'){time++; sec=(int)strtol(time, &time,10);}
  901.   m = (int)strtol (time, &time, 10);
  902. #ifdef CLIENT
  903.   if (!m){
  904.      tmp = TimeControl.clock[computer];
  905.      TimeControl.clock[computer] = t*6000+sec*100;
  906.      timecomp[compptr] += (tmp - TimeControl.clock[computer]);
  907.     } else TimeControl.clock[computer] = t*6000+sec*100;
  908. printf("time %d %d netlag %d %ld %ld\n", TimeControl.clock[computer],TimeControl.moves[computer],tmp - TimeControl.clock[computer],tmp,TimeControl.clock[computer]);
  909. #else
  910.   if (t)
  911.     TimeControl.clock[computer] = t;
  912. #endif
  913.   if (m)
  914.     TimeControl.moves[computer] = m;
  915. #if defined XBOARD && !defined CLIENT
  916.   printz (CP[222], m, t);
  917. #endif
  918. }
  919.  
  920.  
  921. void
  922. InputCommand (void)
  923.  
  924. /*
  925.  * Process the users command. If easy mode is OFF (the computer is thinking
  926.  * on opponents time) and the program is out of book, then make the 'hint'
  927.  * move on the board and call SelectMove() to find a response. The user
  928.  * terminates the search by entering ^C (quit siqnal) before entering a
  929.  * command. If the opponent does not make the hint move, then set Sdepth to
  930.  * zero.
  931.  */
  932.  
  933. {
  934.   int eof = 0;
  935.   short have_shown_prompt = false;
  936.   short ok, tmp;
  937.   unsigned short mv;
  938.   char s[80], sx[80];
  939.  
  940. #if defined CHESSTOOL
  941.   short normal = false;
  942. #endif
  943.  
  944.   ok = flag.quit = false;
  945.   player = opponent;
  946. /* if transposition table is filling start it over */
  947.   if(TTadd > ttbllimit)ZeroTTable();
  948. /* if we have a move for our opponent, and we are thinking on his time, and not in force mode */
  949.   if (hint > 0 && !flag.easy && !flag.force)
  950. /* if the hint is a promotion don't do anything, we don't know what to promote to. */
  951.     if ((board[hint >> 8] != pawn) || ((row (hint & 0x3f) != 0) && (row (hint & 0x3f) != 7)))
  952.       {
  953.     ft = time0;
  954.     fflush (stdout);
  955. /* create the hint move as string so we can do it */
  956.     algbr ((short) hint >> 8, (short) hint & 0x3f, false);
  957.     strcpy (s, mvstr[0]);
  958.     tmp = epsquare;
  959. #ifdef DEBUG12
  960. #include "debug12.h"
  961. #endif
  962. #if !defined CHESSTOOL
  963.     if (flag.post) GiveHint ();
  964. #endif
  965. /* do the hint move */
  966.     if (VerifyMove (s, 1, &mv))
  967.       {
  968.         Sdepth = 0;
  969. #ifdef QUIETBACKGROUND
  970. #ifdef NONDSP
  971.         PromptForMove ();
  972. #else
  973.         ShowSidetoMove ();
  974.         ShowPrompt ();
  975. #endif
  976.         have_shown_prompt = true;
  977. #endif /* QUIETBACKGROUND */
  978. #ifdef CLIENT
  979.     InBackground = true;
  980. #endif
  981. /* would love to put null move in here */
  982. /* after we make the hint move make a 2 ply search with both plys our moves */
  983. /* think on opponents time */
  984.         SelectMove (computer, 2);
  985. #ifdef CLIENT
  986.     InBackground = false;
  987. #endif
  988. /* undo the hint and carry on */
  989.         VerifyMove (s, 2, &mv);
  990.         Sdepth = 0;
  991.       }
  992.     epsquare = tmp;
  993.     time0 = ft;
  994.       }
  995.   while (!(ok || flag.quit))
  996.     {
  997. #if defined CHESSTOOL
  998.       normal = false;
  999. #endif
  1000.       player = opponent;
  1001. #ifdef QUIETBACKGROUND
  1002.       if (!have_shown_prompt)
  1003.     {
  1004. #endif /* QUIETBACKGROUND */
  1005. #ifdef NONDSP
  1006.       PromptForMove ();
  1007. #else
  1008.       ShowSidetoMove ();
  1009.       ShowPrompt ();
  1010. #endif
  1011. #ifdef QUIETBACKGROUND
  1012.     }
  1013.       have_shown_prompt = false;
  1014. #endif /* QUIETBACKGROUND */
  1015. #ifdef NONDSP
  1016.       s[0] = sx[0] = '\0';
  1017.       while (!sx[0])
  1018.     (void) gets (sx);
  1019. #else
  1020.       fflush (stdout);
  1021. #ifdef MSDOS
  1022.       s[0] = '\0';
  1023.       eof = ( gets (sx) == NULL );
  1024. #else
  1025.       eof = ( getstr (sx) == ERR );
  1026. #endif
  1027. #endif
  1028.       sscanf (sx, "%s", s);
  1029.       if (eof)
  1030.     ExitChess ();
  1031.       if (s[0] == '\0')
  1032.     continue;
  1033.       if(s[0] == '!') printf("%s\n",sx);
  1034.       else if (strcmp (s, CP[131]) == 0)    /*bd*/
  1035.     {
  1036. #if defined CHESSTOOL || defined XBOARD
  1037.       chesstool = 0;
  1038. #endif /* CHESSTOOL */
  1039.       ClrScreen ();
  1040.       UpdateDisplay (0, 0, 1, 0);
  1041. #if defined CHESSTOOL || defined XBOARD
  1042.       chesstool = 1;
  1043. #endif /* CHESSTOOL */
  1044.     }
  1045.       else if (strcmp (s, CP[129]) == 0) /* noop */ ;    /*alg*/
  1046.       else if ((strcmp (s, CP[180]) == 0) || (strcmp (s, CP[216]) == 0))    /* quit exit*/
  1047.     flag.quit = true;
  1048.       else if (strcmp (s, CP[178]) == 0)    /*post*/
  1049.     {
  1050.       flag.post = !flag.post;
  1051.     }
  1052.       else if ((strcmp (s, CP[191]) == 0) || (strcmp (s, CP[154]) == 0))    /*set edit*/
  1053.     EditBoard ();
  1054. #ifdef NONDSP
  1055.       else if (strcmp (s, CP[190]) == 0)    /*setup*/
  1056.     SetupBoard ();
  1057. #endif
  1058.       else if (strcmp (s, CP[156]) == 0)    /*first*/
  1059.     {
  1060. #if defined CHESSTOOL
  1061.       computer = white;
  1062.       opponent = black;
  1063.       flag.force = false;
  1064.       Sdepth = 0;
  1065. #endif /* CHESSTOOL */
  1066.       ok = true;
  1067.     }
  1068.       else if (strcmp (s, CP[162]) == 0)    /*go*/
  1069.     {
  1070.       ok = true;
  1071.       flag.force = false;
  1072.       if (computer == white)
  1073.         {
  1074.           computer = black;
  1075.           opponent = white;
  1076.         }
  1077.       else
  1078.         {
  1079.           computer = white;
  1080.           opponent = black;
  1081.         }
  1082.     }
  1083.       else if (strcmp (s, CP[166]) == 0)    /*help*/
  1084.     help ();
  1085.       else if (strcmp (s, CP[221]) == 0)    /*material*/
  1086.     flag.material = !flag.material;
  1087.       else if (strcmp (s, CP[157]) == 0)    /*force*/
  1088.     {flag.force = !flag.force; flag.bothsides = false;}
  1089.       else if (strcmp (s, CP[134]) == 0)    /*book*/
  1090.     Book = Book ? 0 : BOOKFAIL;
  1091.       else if (strcmp (s, CP[172]) == 0)    /*new*/
  1092.     {
  1093.       NewGame ();
  1094.       UpdateDisplay (0, 0, 1, 0);
  1095.     }
  1096.       else if (strcmp (s, CP[171]) == 0)    /*list*/
  1097.     ListGame ();
  1098.       else if (strcmp (s, CP[169]) == 0 || strcmp (s, CP[217]) == 0)    /*level clock*/
  1099.     SelectLevel (sx);
  1100.       else if (strcmp (s, CP[165]) == 0)    /*hash*/
  1101.     flag.hash = !flag.hash;
  1102.       else if (strcmp (s, CP[227]) == 0)    /*gamein*/
  1103.     flag.gamein = !flag.gamein;
  1104.       else if (strcmp (s, CP[226]) == 0)    /*beep*/
  1105.     flag.beep = !flag.beep;
  1106.       else if (strcmp (s, CP[197]) == 0)    /*time*/
  1107.     { SetMachineTime (sx); }
  1108.       else if (strcmp (s, CP[228]) == 0)    /*time*/
  1109.     { SetOppTime (sx); }
  1110.       else if (strcmp (s, CP[33]) == 0)    /*Awindow*/
  1111.     ChangeAlphaWindow ();
  1112.       else if (strcmp (s, CP[39]) == 0)    /*Bwindow*/
  1113.     ChangeBetaWindow ();
  1114.       else if (strcmp (s, CP[183]) == 0)    /*rcptr*/
  1115.     flag.rcptr = !flag.rcptr;
  1116.       else if (strcmp (s, CP[168]) == 0)    /*hint*/
  1117.     GiveHint ();
  1118.       else if (strcmp (s, CP[135]) == 0)    /*both*/
  1119.     {
  1120.       flag.bothsides = !flag.bothsides;
  1121.           flag.force = false;
  1122.       Sdepth = 0;
  1123.       ElapsedTime (1);
  1124.       SelectMove (opponent, 1);
  1125.       ok = true;
  1126.     }
  1127.       else if (strcmp (s, CP[185]) == 0)    /*reverse*/
  1128.     {
  1129.       flag.reverse = !flag.reverse;
  1130.       ClrScreen ();
  1131.       UpdateDisplay (0, 0, 1, 0);
  1132.     }
  1133.       else if (strcmp (s, CP[195]) == 0)    /*switch*/
  1134.     {
  1135.       computer = computer ^ 1;
  1136.       opponent = opponent ^ 1;
  1137.       xwndw = (computer == white) ? WXWNDW : BXWNDW;
  1138.       flag.force = false;
  1139.       Sdepth = 0;
  1140.       ok = true;
  1141.     }
  1142.       else if (strcmp (s, CP[203]) == 0)    /*white*/
  1143.     {
  1144.       computer = black;
  1145.       opponent = white;
  1146.       xwndw = WXWNDW;
  1147.       flag.force = false;
  1148.       Sdepth = 0;
  1149.  
  1150.       /*
  1151.            * ok = true; don't automatically start with white command
  1152.            */
  1153.     }
  1154.       else if (strcmp (s, CP[133]) == 0)    /*black*/
  1155.     {
  1156.       computer = white;
  1157.       opponent = black;
  1158.       xwndw = BXWNDW;
  1159.       flag.force = false;
  1160.       Sdepth = 0;
  1161.  
  1162.       /*
  1163.            * ok = true; don't automatically start with black command
  1164.            */
  1165.     }
  1166.       else if (strcmp (s, CP[201]) == 0 && GameCnt > 0)    /*undo*/
  1167.     {
  1168.       Undo ();
  1169.     }
  1170.       else if (strcmp (s, CP[184]) == 0 && GameCnt > 1)    /*remove*/
  1171.     {
  1172.       Undo ();
  1173.       Undo ();
  1174.     }
  1175.       else if (strcmp (s, CP[160]) == 0)    /*get*/
  1176.     GetGame ();
  1177.       else if (strcmp (s, CP[207]) == 0)    /*xget*/
  1178.     GetXGame ();
  1179.       else if (strcmp (s, CP[189]) == 0)    /*save*/
  1180.     SaveGame ();
  1181.       else if (strcmp (s, CP[151]) == 0)    /*depth*/
  1182.     ChangeSearchDepth ();
  1183. #ifdef DEBUG
  1184.       else if (strcmp (s, CP[147]) == 0)    /*debuglevel*/
  1185.     ChangeDbLev ();
  1186. #endif /* DEBUG */
  1187.       else if (strcmp (s, CP[164]) == 0)    /*hashdepth*/
  1188.     ChangeHashDepth ();
  1189.       else if (strcmp (s, CP[182]) == 0)    /*random*/
  1190.     dither = DITHER;
  1191.       else if (strcmp (s, CP[229]) == 0)    /*hard*/
  1192.     flag.easy = false;
  1193.       else if (strcmp (s, CP[152]) == 0)    /*easy*/
  1194.     flag.easy = !flag.easy;
  1195.       else if (strcmp (s, CP[143]) == 0)    /*contempt*/
  1196.     SetContempt ();
  1197.       else if (strcmp (s, CP[209]) == 0)    /*xwndw*/
  1198.     ChangeXwindow ();
  1199.       else if (strcmp (s, CP[186]) == 0)    /*rv*/
  1200.     {
  1201.       flag.rv = !flag.rv;
  1202.       UpdateDisplay (0, 0, 1, 0);
  1203.     }
  1204.       else if (strcmp (s, CP[145]) == 0)    /*coords*/
  1205.     {
  1206.       flag.coords = !flag.coords;
  1207.       UpdateDisplay (0, 0, 1, 0);
  1208.     }
  1209.       else if (strcmp (s, CP[193]) == 0)    /*stars*/
  1210.     {
  1211.       flag.stars = !flag.stars;
  1212.       UpdateDisplay (0, 0, 1, 0);
  1213.     }
  1214.       else if (strcmp (s, CP[196]) == 0)    /*test*/
  1215.     {
  1216.       ShowMessage (CP[108]);/*test movelist*/
  1217.       TestSpeed (MoveList, 20000);
  1218.       ShowMessage (CP[107]);/*test capturelist*/
  1219.       TestSpeed (CaptureList, 30000);
  1220.       ShowMessage (CP[85]);/*test score position*/
  1221.       TestPSpeed (ScorePosition, 15000);
  1222.     }
  1223.       else
  1224.       if (strcmp (s, CP[179]) == 0)    /*p*/
  1225.     ShowPostnValues ();
  1226.       else if (strcmp (s, CP[148]) == 0)    /*debug*/
  1227.     DoDebug ();
  1228.     else if (strcmp (s, "Mwpawn") == 0)        /*debug*/
  1229.         DoTable (Mwpawn);
  1230.     else if (strcmp (s, "Mbpawn") == 0)        /*debug*/
  1231.         DoTable (Mbpawn);
  1232.     else if (strcmp (s, "Mwknight") == 0)        /*debug*/
  1233.         DoTable (Mknight[white]);
  1234.     else if (strcmp (s, "Mbknight") == 0)        /*debug*/
  1235.         DoTable (Mknight[black]);
  1236.     else if (strcmp (s, "Mwbishop") == 0)        /*debug*/
  1237.         DoTable (Mbishop[white]);
  1238.     else if (strcmp (s, "Mbbishop") == 0)        /*debug*/
  1239.         DoTable (Mbishop[black]);
  1240.       else
  1241.     {
  1242. #if defined CHESSTOOL
  1243.       normal = (ok = VerifyMove (s, 0, &mv));
  1244. #else
  1245.       ok = VerifyMove (s, 0, &mv);
  1246. #endif
  1247.       Sdepth = 0;
  1248.     }
  1249.     }
  1250.  
  1251.   ElapsedTime (1);
  1252.   if (flag.force)
  1253.     {
  1254.       computer = opponent;
  1255.       opponent = computer ^ 1;
  1256.     }
  1257. #if defined CHESSTOOL || defined XBOARD
  1258. #if defined CHESSTOOL
  1259.   if (normal)
  1260.     if (computer == white)
  1261.       printz ("%d. %s", ++mycnt2, s);
  1262.     else
  1263.       printz ("%d. ... %s", ++mycnt2, s);
  1264. #else
  1265.   printz ("%d. %s\n", ++mycnt2, s);
  1266. #endif
  1267. #ifdef notdef /* optional pass best line to frontend with move */
  1268.   if (flag.post)
  1269.     {
  1270.       register int i;
  1271.  
  1272.       printz (" %6d ", MSCORE);
  1273.       for (i = 1; MV[i] > 0; i++)
  1274.     {
  1275.       algbr ((short) (MV[i] >> 8), (short) (MV[i] & 0xFF), false);
  1276.       printz ("%5s ", mvstr[0]);
  1277.     }
  1278.     }
  1279.   printz ("\n");
  1280. #endif
  1281. #endif /* CHESSTOOL */
  1282.   signal (SIGINT, TerminateSearch);
  1283. #ifndef MSDOS
  1284.   signal (SIGQUIT, TerminateSearch);
  1285. #endif /* MSDOS */
  1286. }
  1287.  
  1288. #ifdef HASGETTIMEOFDAY
  1289. void
  1290. ElapsedTime (short int iop)
  1291.  
  1292.  
  1293. /*
  1294.  * Determine the time that has passed since the search was started. If the
  1295.  * elapsed time exceeds the target (ResponseTime+ExtraTime) then set timeout
  1296.  * to true which will terminate the search. iop = 0 calculate et bump ETnodes
  1297.  * iop = 1 calculate et set timeout if time exceeded, calculate et
  1298.  */
  1299.  
  1300. {
  1301. struct timeval tv;
  1302. #ifndef MSDOS
  1303.   int nchar;
  1304.   extern int errno;
  1305.   int i;
  1306. #ifdef FIONREAD
  1307.   if (i = ioctl ((int) 0, FIONREAD, &nchar))
  1308.     {
  1309.       perror ("FIONREAD");
  1310.       fprintf (stderr,
  1311.         "You probably have a non-ANSI <ioctl.h>; see README. %d %d %x\n",
  1312.     i, errno, FIONREAD);
  1313.       exit (1);
  1314.     }
  1315.  
  1316.   if (nchar)
  1317.     {
  1318.       if (!flag.timeout)
  1319.     flag.back = true;
  1320.       flag.bothsides = false;
  1321.     }
  1322. #endif /*FIONREAD*/
  1323. #else
  1324.   if (kbhit ())
  1325.     {
  1326.       if (!flag.timeout)
  1327.     flag.back = true;
  1328.       flag.bothsides = false;
  1329.     }
  1330. #endif /* MSDOS */
  1331.   gettimeofday(&tv,NULL);
  1332.   et = (tv.tv_sec*100+(tv.tv_usec/10000)) - time0;
  1333.   ETnodes = NodeCnt + ZNODES;
  1334.   if (et < 0)
  1335.     et = 0;
  1336.   if (iop == 1)
  1337.     {
  1338.       if (et > ResponseTime + ExtraTime && Sdepth > MINDEPTH)
  1339.     flag.timeout = true;
  1340.       ETnodes = NodeCnt + ZNODES;
  1341.       gettimeofday(&tv,NULL);
  1342.       time0 = tv.tv_sec*100+tv.tv_usec/10000;
  1343.     }
  1344. #if !defined NONDSP
  1345. #ifdef QUIETBACKGROUND
  1346.   if (!background)
  1347. #endif /* QUIETBACKGROUND */
  1348.     UpdateClocks ();
  1349. #endif
  1350. }
  1351. #else
  1352. void
  1353. ElapsedTime (short int iop)
  1354.  
  1355.  
  1356. /*
  1357.  * Determine the time that has passed since the search was started. If the
  1358.  * elapsed time exceeds the target (ResponseTime+ExtraTime) then set timeout
  1359.  * to true which will terminate the search. iop = 0 calculate et bump ETnodes
  1360.  * iop = 1 calculate et set timeout if time exceeded, calculate et
  1361.  */
  1362.  
  1363. {
  1364. #ifndef MSDOS
  1365.   int nchar;
  1366.   extern int errno;
  1367.   int i;
  1368. #ifdef FIONREAD
  1369.   if (i = ioctl ((int) 0, FIONREAD, &nchar))
  1370.     {
  1371.       perror ("FIONREAD");
  1372.       fprintf (stderr,
  1373.         "You probably have a non-ANSI <ioctl.h>; see README. %d %d %x\n",
  1374.     i, errno, FIONREAD);
  1375.       exit (1);
  1376.     }
  1377.  
  1378.   if (nchar)
  1379.     {
  1380.       if (!flag.timeout)
  1381.     flag.back = true;
  1382.       flag.bothsides = false;
  1383.     }
  1384. #endif /*FIONREAD*/
  1385. #else
  1386.   if (kbhit ())
  1387.     {
  1388.       if (!flag.timeout)
  1389.     flag.back = true;
  1390.       flag.bothsides = false;
  1391.     }
  1392. #endif /* MSDOS */
  1393.   et = (time ((long *) 0) - time0) * 100;
  1394.   ETnodes = NodeCnt + ZNODES;
  1395.   if (et < 0)
  1396.     et = 0;
  1397.   if (iop == 1)
  1398.     {
  1399.       if (et > ResponseTime + ExtraTime && Sdepth > MINDEPTH)
  1400.     flag.timeout = true;
  1401.       ETnodes = NodeCnt + ZNODES;
  1402.       time0 = time ((long *) 0);
  1403.     }
  1404. #if !defined NONDSP
  1405. #ifdef QUIETBACKGROUND
  1406.   if (!background)
  1407. #endif /* QUIETBACKGROUND */
  1408.     UpdateClocks ();
  1409. #endif
  1410. }
  1411. #endif
  1412. void
  1413. SetTimeControl (void)
  1414. {
  1415.   if (TCflag)
  1416.     {
  1417.       TimeControl.moves[white] = TimeControl.moves[black] = TCmoves;
  1418.       TimeControl.clock[white] += 6000L * TCminutes + TCseconds * 100;
  1419.       TimeControl.clock[black] += 6000L * TCminutes + TCseconds * 100;
  1420.     }
  1421.   else
  1422.     {
  1423.       TimeControl.moves[white] = TimeControl.moves[black] = 0;
  1424.       TimeControl.clock[white] = TimeControl.clock[black] = 0;
  1425.     }
  1426.   flag.onemove = (TCmoves == 1);
  1427.   et = 0;
  1428.   ElapsedTime (1);
  1429. }
  1430.